#!/bin/bash

# SINDV2XHTML RUN SCRIPT
# Usage: sindb2xhtml -i [path to docbook slides] -o [output directory]

SINDB2XHTML_JARFILENAME=sindb2xhtml-0.2-jar-with-dependencies.jar

SINDB2XHTML_HOME=`dirname $0` 			#location of this script

#      SAXON=$SINDB2XHTML_HOME/docbook/slides/xhtml/saxon.jar
SINDB2XHTML=$SINDB2XHTML_HOME/target/$SINDB2XHTML_JARFILENAME

#CP=$SAXON:$SINDB2XHTML	
CP=$SINDB2XHTML
PROPERTIES=$SINDB2XHTML_HOME/settings.properties

help () {
	echo "SINDB2XHTML 2011 runscript Help"
	echo "The arguments to use are:"
	echo -e "\t-i [path to input file]"
	echo -e "\t-o [path to output directory]"
	echo -e "\t-h display this help"
}

OUTDIR="./"
INFILE=""
B_DISPLAY_HELP=N
B_IN_SET=N
B_OUT_SET=N

#process script arguments
if [ $# = 0 ]
then
	help
	exit 1
fi

while [ $# -gt 0 ] 
do 
	case $1 in 
	-i) INFILE="$2"
	    shift 2
 	    B_IN_SET=Y;;

	-o) OUTDIR="$2"
	    shift 2
	    B_OUT_SET=Y;;

	-h) B_DISPLAY_HELP=Y 
	    shift 1;;

	 *) B_DISPLAY_HELP=Y
	    shift 1;;

	esac
done
if [ $B_OUT_SET = "N" ]
then
	echo "Warning: output directory was not set, using actual working dir for output"
fi

if [ $B_IN_SET = "N" ]
then
	echo "Error: input file was not set, aborting"
	echo "Use $0 -h to display help"
	exit 2
fi

if [ $B_DISPLAY_HELP = "Y" ]
then
	help
	exit 0
fi

if [ -e "$INFILE" ]
then
	INFILE=$(readlink -f "$INFILE") #absolute path
else
	echo "File $INFILE doesn't exist"
	exit 3
fi

if [ ! -d $OUTDIR ]
then
	echo "Directory $OUTDIR doesn't exist or it isn't a directory"
	exit 4
fi

echo "Changing working directory to $OUTDIR"
cd $OUTDIR

java -cp $CP cz.muni.fi.sindb2xhtml.Sindb2xhtml -s "$INFILE" 
#-p $PROPERTIES



